/**
 * 
 */

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertArrayEquals;


import java.util.Arrays;



import org.junit.Before;
import org.junit.Test;



/**
 * @author Stanley Ayzenberg
 * @version 1.0
 */
public class SaveLoadTest {
	/**
	 * Field saveLoad.
	 */
	private SaveLoad saveLoad;

	/**
	 * Field gui.
	 */
	private SpaceTraderGui gui;

	/**
	 * Field testPlayer.
	 */
	private Player testPlayer;

	/**
	 * Field otherPlayer.
	 */
	private Player otherPlayer;

	/**
	 * Field newPlayer.
	 */
	private Player newPlayer;

	/**
	 * Field currSystem.
	 */
	private SolarSystem currSystem;

	/**
	 * Field otherSystem.
	 */
	private SolarSystem otherSystem;

	/**
	 * Field newSystem.
	 */
	private SolarSystem newSystem;

	/**
	 * Field market.
	 */
	private Marketplace market;

	/**
	 * Field otherMarket.
	 */
	private Marketplace otherMarket;

	/**
	 * Field newMarket.
	 */
	private Marketplace newMarket;
	
	/**
	 * Initial skillpoints
	 */
	private static final int INIT_SP = 16;
	
	/**
	 * Test skillpoints
	 */
	private static final int TEST_SP = 4;
	
	/**
	 * Starting credits
	 */
	private static final int INIT_CRED = 1000;
	
	/**
	 * Starting credits 2
	 */
	private static final int INITCRED_TWO = 500;
	
	/**
	 * toString method.
	
	 * @return String */
	public String toString() {
		return "";
	}
	
	/**
	 * Method start.
	 */
	@Before
	public void start() {
		gui = new SpaceTraderGui();
		testPlayer = new Player("Test", INIT_SP, TEST_SP, TEST_SP, TEST_SP, INIT_CRED);
		gui.startGame(testPlayer);
		currSystem = gui.getCurrentSystem();
		market = gui.getMarketplace();
		saveLoad = new SaveLoad(gui);
		saveLoad.saveGame();
		gui.startGame(new Player("Test2", TEST_SP, TEST_SP,
				TEST_SP, TEST_SP, INITCRED_TWO));
		gui.setCurrentSystem(new SolarSystem("Test"));
		otherPlayer = gui.getPlayer();
		otherSystem = gui.getCurrentSystem();
		otherMarket = gui.getMarketplace();
		saveLoad.loadGame();
		newPlayer = gui.getPlayer();
		newSystem = gui.getCurrentSystem();
		newMarket = gui.getMarketplace();
	}
	
		/**
		 * Method testName.
		 */
		@Test
		public void testName() {
		final boolean matches = testPlayer.getName().equals(otherPlayer.getName());
		assertEquals("assertion failed", testPlayer.getName(), newPlayer.getName());
		assertFalse("assertion failed", matches);
	}

		/**
		 * Method testCredits.
		 */
		@Test
		public void testCredits() {
		final boolean matches = testPlayer.getCredits() == otherPlayer.getCredits();
		assertEquals("assertion failed", testPlayer.getCredits(), newPlayer.getCredits());
		assertFalse("assertion failed", matches);
	}
	
		/**
		 * Method testPoints.
		 */
		@Test
		public void testPoints() {
		final boolean matches1 = testPlayer.getEngineerPoints()
				== otherPlayer.getEngineerPoints();
		final boolean matches2 = testPlayer.getFightPoints()
				== otherPlayer.getFightPoints();
		final boolean matches3 = testPlayer.getPilotPoints()
				== otherPlayer.getPilotPoints();
		final boolean matches4 = testPlayer.getTraderPoints()
				== otherPlayer.getTraderPoints();
		assertEquals("assertion failed", testPlayer.getEngineerPoints(), 
				newPlayer.getEngineerPoints());
		assertEquals("assertion failed", testPlayer.getFightPoints(), 
				newPlayer.getFightPoints());
		assertEquals("assertion failed", testPlayer.getPilotPoints(), 
				newPlayer.getPilotPoints());
		assertEquals("assertion failed", testPlayer.getTraderPoints(), 
				newPlayer.getTraderPoints());
		assertFalse("assertion failed", matches1);
		
		assertFalse("assertion failed", matches2);
		assertFalse("assertion failed", matches3);
		assertFalse("assertion failed", matches4);
	}
	
		/**
		 * Method testShips.
		 */
		@Test
		public void testShips() {
		final SpaceShip testShip = testPlayer.getShip();
		final SpaceShip newShip = newPlayer.getShip();
		assertEquals("assertion failed", testShip.getFuel(), newShip.getFuel());
		assertArrayEquals(testShip.getGoods(), newShip.getGoods());
		assertEquals("assertion failed", testShip.getCargo(), newShip.getCargo());
	}
	
		/**
		 * Method testSystems.
		 */
		@Test
		public void testSystems() {
		final boolean matches = currSystem.getName().equals(otherSystem.getName());
		assertEquals("assertion failed", currSystem.getName(), newSystem.getName());
		assertFalse("assertion failed", matches);
	}
	
		/**
		 * Method testMarketplace.
		 */
		@Test
		public void testMarketplace() {
		final boolean matches1 = Arrays.equals(market.getBuyPrices(),
				(otherMarket.getBuyPrices()));
		final boolean matches2 = Arrays.equals(market.getSellPrices(),
				(otherMarket.getSellPrices()));
		assertArrayEquals(market.getBuyPrices(), newMarket.getBuyPrices());
		assertArrayEquals(market.getSellPrices(), newMarket.getSellPrices());
		assertFalse("assertion failed", matches1);
		assertFalse("assertion failed", matches2);
	}
	

}
